package com.baosight.sczx.common.zm.cInterface.ce;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.TablePk;

/**
 * 标准设备接口
 * <p>Title: StandardEquipment.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-4-12 下午05:08:05  张志芳  Create
 *</pre>
 */
public class StandardEquipment {		
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 判断是否存在标准设备，存在返回标准设备的ID，不存在返回空
	 * <p>Function: containsStandard</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午12:40:44 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String containsStandard(String name, String model, String specifications)
	{
		String keyId = "";
		try{
			List<Map>  _list = new ArrayList();
			Map para = new HashMap();
			para.put("fdName", name);
			para.put("fdSpecifications", specifications);
			para.put("fdModel", model);
			_list = dao.query("ZMCEStandredEquipment.queryStandardId", para);
			if(_list.size()>0)
			{
				Map map = _list.get(0);
				keyId = map.get("fdKeyId").toString();
			}
		}catch(Exception e){
			
		}
		return keyId;
	}
	
	/**
	 * (重载)判断是否存在标准设备，存在返回标准设备的ID，不存在返回空
	 * <p>Function: containsStandard</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午01:32:28 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String containsStandard(String name, String model, String specifications, String classId)
	{
		String keyId = "";
		try{
			List<Map>  _list = new ArrayList();
			Map para = new HashMap();
			para.put("fdName", name);
			para.put("fdSpecifications", specifications);
			para.put("fdModel", model);
			para.put("fdEquipmentClassId", classId);
			_list = dao.query("ZMCEStandredEquipment.queryStandardId", para);
			if(_list.size()>0)
			{
				keyId = _list.get(0).get("fdKeyId").toString();
			}
		}catch(Exception e){
			
		}
		return keyId;
	}
	
	/**
	 * 新增不存在的标准设备,返回新增的主键
	 * <p>Function: containsStandard</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午01:32:28 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String insertStandard(String name, String model, String specifications, String classId, String toencode)
	{
		String keyId = "";
		try{
			keyId = containsStandard(name, model, specifications);
			if(keyId.equals(""))
			{
				keyId=TablePk.getInstance().getNextValue("T_CE_STANDREDEQUIPMENT");
				Map para = new HashMap();
				para.put("fdKeyId", keyId);
				para.put("fdName", name);
				para.put("fdSpecifications", specifications);
				para.put("fdModel", model);
				para.put("fdEquipmentClassId", classId);
				para.put("fdToencode", toencode);
				dao.insert("ZMCEStandredEquipment.insert", para);
			}
		}catch(Exception e){
			
		}
		return keyId;
	}
	
	/**
	 * 根据设备Id删除所有的标准
	 * <p>Function: clearAllStandard</p>
	 * <p>Description: </p>
	 * <p>return value:Boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午05:09:37 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	public static Boolean clearAllStandard(String equipmentId)
	{
		try{
			List<Map>  _list = new ArrayList();
			Map para = new HashMap();
			para.put("fdKeyId", equipmentId);
			dao.delete("ZMCEMaintenanceStandard.deleteByEquipment", para);
			dao.delete("ZMCELubricationStandard.deleteByEquipment", para);
			dao.delete("ZMCERepairStandard.deleteByEquipment", para);
			dao.delete("ZMCEPointStandard.deleteByEquipment", para);
			
			return true;
		}catch(Exception e){
			
		}
		return false;
	}
		
	/**
	 * 判断某个设备分类下是否有标准设备存在
	 * <p>Function: hasEquipment</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21下午04:39:08 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static boolean hasEquipment(String classId)
	{
		try {
			Map params = new HashMap();
			params.put("fdEquipmentClassId", classId);
			List<Map>  _list  = dao.query("ZMCEStandredEquipment.queryInterface", params);
			if(_list.size()>0)
				return true;
			else
				return false;
		} catch (Exception e) {
			
		}
		return true;
	}
}

